<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Luckydraw extends MY_Luckydog 
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('luckydog/Luckydraw_model', 'luckydraw');
    }
	public function index()
	{
        $_SESSION['v_menu'] = 'luckydraw';
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $filter = [];
        $total_rows = $this->luckydraw->getLuckydrawCnt($filter);
        $base_url = '/luckydog/luckydraw/index?';
        $limit = 20;
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $luckydrawlist = $this->luckydraw->get_luckydraw_list($filter,$per_page, $limit);
        $total_chance = 0;
        $total_online_row = 0;
        foreach ($luckydrawlist as $key => $value) {
            $coverInfo =  $this->luckydraw->get_img_list_byid($value['id']);
            $luckydrawlist[$key]['cover'] = $coverInfo[0]['imgurl'];
            if($value['onlinestatus'] == 1)
            {
                $total_chance += intval($value['chance']);
                $total_online_row += 1;
            }
            
        }
        $data=array(
            'luckydraw_list'=>$luckydrawlist,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'title'=>"奖品列表",
            'total_chance'=>$total_chance,
            'total_online_row'=>$total_online_row
        );
        

		$this->load->view('luckydog/luckydraw/luckydraw_list',$data);
	}
	
	public function chooseUser()
    {
        $_SESSION['v_menu'] = 'chooseuser';
        $starttime = $this->input->get("starttime",true)?$this->input->get("starttime",true):"";
        $endtime = $this->input->get("endtime",true)?$this->input->get("endtime",true):"";
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $status = $this->input->get('status');
        $name = $this->input->get('name') ? $this->input->get('name') : "";
        $filter = array(
            'starttime'=>($starttime),
            'endtime'=>($endtime),
            'status'=>$status,
        );
		
        $limit = 20;
        $total_rows = $this->luckydraw->getChooseUserCnt($filter);
        $base_url = '/luckydog/luckydraw/chooseuser?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $userList = $this->luckydraw->getChooseUserList($filter,$per_page,$limit);
        foreach ($userList as $key => $value) {
            $userSelected = $this->luckydraw->checkUserSelected($value['AccountID']);
            if($userSelected)
            {
                $userList[$key]['userselected'] = 1;
            }else{
                $userList[$key]['userselected'] = 0;
            }
        }
        $data = array(
            'userList'=>$userList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'status'=>$status,
			'starttime'=>$starttime,
			'endtime'=>$starttime
        );
        $this->load->view('/luckydog/luckydraw/chooseuserlist',$data);
    } 


    //添加分类 html
    public function add()
    {
        $this->load->model("address_model","address");
        $fields = " id, name ";
        //基本信息
        $provinceList = array();
        $provinceList = $this->address->getProvinceList($fields);
        $provinceId = $newsInfo['provinceid']?$newsInfo['provinceid']:110000;
        $cityList = array();
        $cityList = $this->address->getCityList($provinceId, $fields);

        // print_R($provinceList);
        $other_chance = $this->luckydraw->getChance();
        $data['mychance'] = 100-$other_chance;  
        $data['catList'] = $catList;
        $data['provinceList'] = $provinceList;
        $data['cityList'] = $cityList;
        $this->load->view('luckydog/luckydraw/luckydrawext',$data);
    }
    //修改分类 html
    public function edit()
    {
        $id  = $this->input->get("id");
        if (empty($id)){
            show_message("参数错误~, <a class='poi' onclick='javascript:history.go(-1);'>返回>></a>");
            exit;
        }
        $luckydrawdetail = $this->luckydraw->get_luckydraw_by_id($id);
        $luckydraw_img_list = $this->luckydraw->get_img_list_byid($id);

        $data=array(
            'luckydrawdetail'=>$luckydrawdetail,
            'luckydraw_img_list'=>$luckydraw_img_list,
        );
        //基本信息
        $this->load->model("address_model","address");
        $fields = " id, name ";
        $provinceList = array();
        $provinceList = $this->address->getProvinceList($fields);
        $provinceId = $luckydrawdetail['provinceid']?$luckydrawdetail['provinceid']:110000;
        $cityList = array();
        $cityList = $this->address->getCityList($provinceId, $fields);
        $other_chance = $this->luckydraw->getOtherChance($id);
        $data['mychance'] = 100-$other_chance;
        $data['catList'] = $catList;
        $data['provinceList'] = $provinceList;
        $data['cityList'] = $cityList;
        $this->load->view('luckydog/luckydraw/luckydrawext',$data);
    }
   
    public function do_add(){
        $title   = $this->input->post("title");
        $price   = $this->input->post("price");
        $summary   = $this->input->post("summary");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");
        $companyid = $this->input->post("companyid");
        $provinceid = $this->input->post("provinceid");
        $cityid = $this->input->post("cityid");
        $area = $this->input->post("area");
        $gettype = $this->input->post("gettype");
        $isempty = $this->input->post("isempty");
        $chance = $this->input->post("chance");
        $stock = $this->input->post("stock");

        $data = array(
            "title"=>$title,
            "price"=>$price,
            "summary"=>$summary,
            "addtime"=>time(),
            "provinceid"=>$provinceid,
            "cityid"=>$cityid,
            "area"=>$area,
            "gettype"=>$gettype,
            "isempty"=>$isempty,
            "chance"=>$chance,
            "stock"=>$stock,
        );
        $insert_id = $this->luckydraw->add_luckydraw($data);
        //添加图书背景图片
        $status = 0;
        if($insert_id && !empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                $temp = array(
                    "luckydrawid"=>$insert_id,
                    "imgurl"=>$value,
                    "is_default"=>$default_arr[$key]
                    );
                $this->luckydraw->add_luckydraw_img($temp);
            }
        }
        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加失败'));
        }
        exit;
    }
    public function do_edit(){
        $luckydrawid   = $this->input->post("id");
        $title   = $this->input->post("title");
        $price   = $this->input->post("price");
        $summary   = $this->input->post("summary");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");
        $ids_arr = $this->input->post("ids_arr");
        $provinceid = $this->input->post("provinceid");
        $cityid = $this->input->post("cityid");
        $area = $this->input->post("area");
        $gettype = $this->input->post("gettype");
        $isempty = $this->input->post("isempty");
        $chance = $this->input->post("chance");
        $stock = $this->input->post("stock");

        $data = array(
            "title"=>$title,
            "price"=>$price,
            "summary"=>$summary,
            "provinceid"=>$provinceid,
            "cityid"=>$cityid,
            "area"=>$area,
            "gettype"=>$gettype,
            "isempty"=>$isempty,
            "chance"=>$chance,
            "stock"=>$stock,
        );

        if (empty($luckydrawid)){
            echo json_encode(array('status'=>-1,'message'=>'参数错误！'));
            exit;
        }
        $edit_flag = $this->luckydraw->edit_luckydraw($luckydrawid,$data);


         //添加图书背景图片
        $status = 0;
        if(!empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                if($ids_arr[$key]){
                    //执行修改
                    $temp = array(
                        "is_default"=>$default_arr[$key]
                    );
                    $res = $this->luckydraw->edit_luckydraw_img($ids_arr[$key],$temp);
                }else{
                    //执行添加
                    $temp = array(
                        "luckydrawid"=>$luckydrawid,
                        "imgurl"=>$value,
                        "is_default"=>$default_arr[$key]
                    );
                    $this->luckydraw->add_luckydraw_img($temp);
                }
            }
        }

        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'商品修改成功'));
        }else{
            echo json_encode(array('status'=>-2,'message'=>'商品修改失败'));
        }
        exit;
    }
    public function do_del(){
        $id  = $this->input->post("id");
        $this->luckydraw->del_luckydraw($id);
        echo json_encode(array('status'=>1));
        exit;
    }


    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);

        $savepath='./upload/ad/'.date('Y-m-d').'/';
        $upload->set_upload_dir($savepath);
        if (!file_exists($savepath)){
            mkdir($savepath);
        }
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }

    /**
     * 删除图片
     */
    public function del_pic()
    {

        $this->load->library('upload');
        $imgsource = $this->input->post('imgsource', TRUE) ? $this->input->post('imgsource', TRUE) : '';

        //删除图片文件
        if (!empty($imgsource)) {
            $upload = new Upload();
            $res = $upload->del_file($imgsource);
        }
        //删除数据库数据
        $id = $this->input->post('id', TRUE) ? $this->input->post('id', TRUE):0;
        if($id){
            $res = $this->luckydraw->del_attach($id);    
        }
        
        echo $res;

    }
    //编辑上线下，删除状态
    public function editStatus(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $onlinestatus = $this->input->post("onlinestatus",true) ? $this->input->post("onlinestatus",true) : "";
        $type = $this->input->post("type",true) ? $this->input->post("type",true) : 0;
        $data = array(
            "id"=>$id,
            "onlinestatus"=>$onlinestatus,
            "type"=>$type
        );
        $res =$this->luckydraw->editStatus($data);

        $id = $this->input->post('id', TRUE) ? $this->input->post('id', TRUE):0;
        $res = $this->luckydraw->del_attach($id);
        echo $res;
    }

    public function detail(){
        $luckydrawid = $this->input->get("luckydrawid",true) ? $this->input->get("luckydrawid",true) : 0;
        $luckydrawdetail = $this->luckydraw->get_luckydraw_by_id($luckydrawid);
        $companyInfo = $this->company->get_company_by_id($luckydrawdetail['companyid']);
        $luckydrawdetail['companyName'] = $companyInfo['name'];
        $data=array(
            'luckydrawdetail'=>$luckydrawdetail
        );
        $this->load->view('luckydog/luckydraw/luckydraw_detail',$data);
    }

    public function orderdetail(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $orderdetail = $this->luckydraw->get_order_by_id($id);
        $data=array(
            'orderdetail'=>$orderdetail
        );
        $this->load->view('luckydog/luckydraw/order_detail',$data);
    }



     public function orderList(){
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $starttime = $this->input->get("starttime",true)?$this->input->get("starttime",true):"";
        $endtime = $this->input->get("endtime",true)?$this->input->get("endtime",true):"";
        $keyword = $this->input->get("keyword",true)?$this->input->get("keyword",true):"";
        $choosestatus = $this->input->get("choosestatus",true)?$this->input->get("choosestatus",true):"";
        $gettype = $this->input->get("gettype",true)?$this->input->get("gettype",true):"";
        $_SESSION['v_menu'] = 'luckydraw';
        $limit = 20;
        $filter = array(
            'starttime'=>strtotime($starttime),
            'endtime'=>strtotime($endtime),
            'keyword'=>$keyword,
            'status'=>$choosestatus,
            'gettype'=>$gettype,
            );
        $total_rows = $this->luckydraw->getOrderCnt($filter);
        $base_url = '/luckydog/luckydraw/orderList?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $orderList = $this->luckydraw->getOrderList($filter,$per_page, $limit);
        $data = array(
            'orderList'=>$orderList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            'type'=>$type,
            'title'=>"获奖者列表"
            );
        $this->load->view("/luckydog/luckydraw/orderlist", $data);
    }

    public function do_orderdel(){
        $id  = $this->input->post("id");
        $this->luckydraw->del_order($id);
        echo json_encode(array('status'=>1));
        exit;
    }
     //编辑状态信息
    public function edit_order_status(){
        $status = $this->input->post("status",true)?$this->input->post("status",true):"";
        $id = $this->input->post("id",true)?$this->input->post("id",true):"";
        $res = $this->luckydraw->setOrderStatus($id,$status);
       echo $res;
    }

    public function edit_express(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $data=array(
            'id'=>$id
        );
        $this->load->view('/luckydog/luckydraw/edit_express',$data);
    }

    public function addLuckydrawUser(){
        $chooseuser = $this->input->post("chooseuser",true) ? $this->input->post("chooseuser",true) : [];
        $current_batch = $this->input->post("current_batch",true) ? $this->input->post("current_batch",true) : '';
        if($chooseuser)
        {
            foreach ($chooseuser as $key => $value) {
                $this->luckydraw->addLuckydrawUser($value,$current_batch);
            }
        }
         echo 1;
    }
    public function deleteLuckydrawUser(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res = $this->luckydraw->deleteLuckydrawUser($id);
         echo $res;
    }
    public function do_editExpress(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $expressName = $this->input->post("expressName",true) ? $this->input->post("expressName",true) : 0;
        $expressNo = $this->input->post("expressNo",true) ? $this->input->post("expressNo",true) : 0;
        $data=array(
            'expressName'=>$expressName,
            'expressNo'=>$expressNo,
            'status'=>2,
        );
        $res = $this->luckydraw->editExpress($id,$data);
        echo $res;
    }

}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */
